//
//  UIImage+Color.m
//  Shuaiche
//
//  Created by dangshuai on 2018/12/18.
//  Copyright © 2018 zzs. All rights reserved.
//

#import "UIImage+Color.h"

@implementation UIImage (Color)

+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width,size.height);
    UIGraphicsBeginImageContext(size);//创建图片
    CGContextRef context = UIGraphicsGetCurrentContext();//创建图片上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);//设置当前填充颜色的图形上下文
    CGContextFillRect(context, rect);//填充颜色
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

+ (UIImage *)blurImage {
    
    CGRect rect = CGRectMake(0, 0, kSCScreenWidth, 100);
    
    UIGraphicsBeginImageContext(rect.size);//创建图片
    CGContextRef context = UIGraphicsGetCurrentContext();//创建图片上下文
    CGContextSetFillColorWithColor(context, [SC_RGB(51, 51, 51) CGColor]);//设置当前填充颜色的图形上下文
    CGContextFillRect(context, rect);//填充颜色
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CIImage *ciImage = [CIImage imageWithCGImage:theImage.CGImage];
    
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:ciImage forKey:kCIInputImageKey];
    [filter setValue:@5 forKey:kCIInputRadiusKey];
    
//    CIContext *cicontext = [CIContext contextWithOptions:nil];
//    CIImage *result = [filter valueForKey:kCIOutputImageKey];
//            CGRect frame = [ciImage extent];
//            NSLog(@"%f,%f,%f,%f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
//            CGImageRef outImage = [cicontext createCGImage: result fromRect:ciImage.extent];
//            UIImage * blurImage = [UIImage imageWithCGImage:outImage];
    return [UIImage imageWithCIImage:filter.outputImage];
}

+ (UIImage *)imageWithColor:(UIColor *)color radius:(CGFloat)r {
    
    CGFloat w = 4 * r;
    CGFloat h = 2 * r;
    CGRect rect = CGRectMake(0.0f, 0.0f, w,h);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:r];
    UIGraphicsBeginImageContext(CGSizeMake(w, h));//创建图片
    CGContextRef context = UIGraphicsGetCurrentContext();//创建图片上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);//设置当前填充颜色的图形上下文
    CGContextAddPath(context, path.CGPath);
    CGContextFillPath(context);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
    
}
+ (UIImage *)imageWithColor:(UIColor *)color rect:(CGRect)rect radius:(CGFloat)r {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:r];
    UIGraphicsBeginImageContext(CGSizeMake(rect.size.width, rect.size.height));//创建图片
    CGContextRef context = UIGraphicsGetCurrentContext();//创建图片上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);//设置当前填充颜色的图形上下文
    CGContextAddPath(context, path.CGPath);
    CGContextFillPath(context);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1,1);
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));//创建图片
    CGContextRef context = UIGraphicsGetCurrentContext();//创建图片上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);//设置当前填充颜色的图形上下文
    CGContextFillRect(context, rect);//填充颜色
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
